package js;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.commons.dbutils.DbUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class DBUtils {

	private String dbip;
	private String uid;
	private String pwd;
	private String dbname;
	private Connection connection = null;

	private static Logger log = LogManager.getLogger(DBUtils.class);

	DBUtils(){
		this.dbip = StatsConfig.dbip;
		this.dbname = StatsConfig.dbname;
		this.uid = StatsConfig.uid;
		this.pwd = StatsConfig.pwd;
	}

	public  Connection getConnection() throws SQLException{
//		String strConnString = "jdbc:oracle:thin:@//" + dbip + ":1521/" + dbname;
		String strConnString = dbip;
		DbUtils.loadDriver("oracle.jdbc.driver.OracleDriver");
	    log.debug("ConnStr:" + strConnString + ",uid:" + uid );
	    connection = DriverManager.getConnection(strConnString, uid, pwd);
		return connection;
	}

	public Connection getConn()
	{
		try
		{
			if (connection == null)
			{
				getConnection();
				log.info("�Ѿ���ȡ���ݿ�����");
			}
			return connection;
		}
		catch (Exception e)
		{
			log.error("��ȡ���ݿ�����ʧ��!", e.fillInStackTrace());
			return null;
		}
	}

	public void close()
	{
		try
		{
			if (connection != null)
			{
				connection.close();
				connection = null;
				log.info("�Ѿ��ر����ݿ�����");
			}
		}
		catch (Throwable e)
		{
			log.error("�ر����ݿ�����ʧ��! ", e.fillInStackTrace());
		}
	}

}
